package com.chainwintel.prjmgmt.service;

import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.chainwintel.prjmgmt.model.MenuCategory;
import com.chainwintel.prjmgmt.repository.GenericRepositoryImpl;

@Repository
@Transactional(readOnly = true)
public class MenuCategoryMgrImpl extends GenericRepositoryImpl<MenuCategory>
		implements MenuCategoryMgr {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -4211500387940428827L;

	@Transactional
	public void edit(MenuCategory parentCategory, MenuCategory category)
			throws DataAccessException {
		// TODO Auto-generated method stub
		parentCategory = getEntityManager().merge(parentCategory);
		category = getEntityManager().merge(category);
		
		// 为每一个目录节点添加一个html checkbox
		category.setName("<input type='checkbox' name='selectedNodeIds' value = '" + category.getId() + "' />" + category.getName());
		category.setParentCategory(parentCategory);
	}

	@SuppressWarnings("unchecked")
	public List<MenuCategory> listRootCategories() throws DataAccessException {
		// TODO Auto-generated method stub
		return getEntityManager().createQuery("select c from MenuCategory c where c.parentCategory is null").getResultList();
	}

	public MenuCategory find(long id) throws DataAccessException {
		// TODO Auto-generated method stub
		return find(MenuCategory.class, id);
	}

}
